package me.firstapp.service.bbs.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Member;
import me.firstapp.module.bbs.Section;
import me.firstapp.module.bbs.Topic;
import me.firstapp.repository.bbs.MemberRepository;
import me.firstapp.repository.bbs.SectionRepository;
import me.firstapp.repository.bbs.TopicRepository;
import me.firstapp.service.bbs.TopicService;

@Service
public class TopicServiceImpl implements TopicService {

	@Autowired
	private TopicRepository topicRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private SectionRepository sectionRepository;

	public Topic addTopic(Long memberId, Long sectionId, String title, String summary, String content,
			Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		if (sectionId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "板块id");
		}
		if (StrUtils.isNULL(title)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "标题");
		}
		if (StrUtils.isNULL(summary)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "摘要");
		}
		Member member = (Member) memberRepository.findMemberById(memberId, apiClientId);
		if (member == null) {
			throw new ServiceException(StatusHouse.MEMBER_NOT_FOUND);
		}
		if (member.getStatus() == 0) {
			throw new ServiceException(StatusHouse.MEMBER_NOT_USE);
		}
		Section section = (Section) sectionRepository.findSectionById(sectionId, apiClientId);
		if (section == null) {
			throw new ServiceException(StatusHouse.SECTION_NOT_FOUND);
		}
		Topic topic = topicRepository.findTopicByTitle(title, apiClientId);
		if (topic != null) {
			throw new ServiceException(StatusHouse.TOPIC_IS_EXIST);
		}

		topic = new Topic();
		topic.setMember(member);
		topic.setSection(section);
		topic.setTitle(title);
		topic.setSummary(summary);
		topic.setContent(content);
		topic.setCreateTime(Calendar.getInstance().getTime());
		topic.setRecommend(0);
		topic.setApiClientId(apiClientId);
		topicRepository.save(topic);
		return topic;
	}

	@Override
	public Topic findTopicById(Long topicId, Long apiClientId) {
		if (topicId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "话题id");
		}
		return topicRepository.findTopicById(topicId, apiClientId);
	}

	@Override
	public Topic findTopicByTitle(String title, Long apiClientId) {
		return topicRepository.findTopicByTitle(title, apiClientId);
	}

	@Override
	public Long findTopicsCountsByMemberId(Long memberId, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		return topicRepository.findTopicsCountsByMemberId(memberId, apiClientId);
	}

	@Override
	public List<Topic> findSomeTopics(Long memberId, Long sectionId, Integer counts, Long apiClientId) {
		return topicRepository.findSomeTopics(memberId, sectionId, counts, apiClientId);
	}

	@Override
	public Page<Topic> findTopicsPageByParams(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Date startTime, Date endTime, Long apiClientId) {
		return topicRepository.findTopicsPageByParams(pageNo, pageSize, searchParams, sortParam, sortType, startTime,
				endTime, apiClientId);
	}

	@Override
	public void deleteTopics(String ids, Long apiClientId) {
		if (StrUtils.isNULL(ids)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "话题id");
		}
		if (!ids.contains(",")) {
			Topic topic = topicRepository.findTopicById(Long.valueOf(ids), apiClientId);
			if (topic != null) {
				topicRepository.deleteById(Long.valueOf(ids));
			}
		} else {
			String[] _ids = ids.split(",");
			Long[] _ids_ = new Long[_ids.length];
			for (int i = 0; i < _ids.length; i++) {
				Topic topic = topicRepository.findTopicById(Long.valueOf(_ids[i]), apiClientId);
				if (topic != null) {
					_ids_[i] = Long.valueOf(_ids[i]);
				}
			}
			if (_ids.length > 0) {
				topicRepository.delete(_ids_);
			}
		}
	}

	@Override
	public Page<Topic> findTopicsPage(Integer pageNo, Integer pageSize, Long sectionId, Long memberId,
			Integer recommend, String sortParam, String sortType, Long apiClientId) {
		return topicRepository.findTopicsPage(pageNo, pageSize, sectionId, memberId, recommend, sortParam, sortType,
				apiClientId);
	}

	@Override
	public Topic updateTopic(Long topicId, String summary, String content, Long upCounts, Long viewCounts,
			Long replyCounts, Long collectCounts, Integer recommend, Long apiClientId) {
		if (topicId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "话题id");
		}
		Topic topic = (Topic) topicRepository.findTopicById(topicId, apiClientId);
		if (topic == null) {
			throw new ServiceException(StatusHouse.TOPIC_NOT_EXIST);
		}
		if (StrUtils.isNotNULL(summary)) {
			topic.setSummary(summary);
			topic.setModifyTime(new Date());
		}
		if (StrUtils.isNotNULL(content)) {
			topic.setContent(content);
			topic.setModifyTime(new Date());
		}
		if (upCounts != null) {
			topic.setUpCounts(upCounts + topic.getUpCounts());
		}
		if (viewCounts != null) {
			topic.setViewCounts(viewCounts + topic.getViewCounts());
		}
		if (replyCounts != null && replyCounts != 0) {
			topic.setReplyCounts(replyCounts);
		}
		if (collectCounts != null && collectCounts != 0) {
			topic.setCollectCounts(collectCounts);
		}
		if (recommend != null) {
			topic.setRecommend(recommend);
		}

		topicRepository.update(topic);

		return topic;
	}

	@Override
	public Long findTopicsCountsBySectionId(Long sectionId, Long apiClientId) {
		if (sectionId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "板块id");
		}
		return topicRepository.findTopicsCountsBySectionId(sectionId, apiClientId);
	}

}
